//
//  ViewController.m
//  10--HomeWork
//
//  Created by 冀永金 on 16/7/27.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "ViewController.h"
#import "FirstTableViewCell.h"
#import "SecondTableViewCell.h"
#import "Album.h"
#define WIDTH self.view.frame.size.width
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UIView *viewOfHeader;
@property (nonatomic, strong) UITableView *carTableView;
@property (nonatomic, strong) NSArray *arrOfCar;
@property (nonatomic, strong) NSArray *imageArr;
@property (nonatomic, strong) NSMutableArray *marr;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"YiChe" ofType:@"plist"];
   
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];
    //NSLog(@"------%@",[dic objectForKey:@"data"] );
   self.arrOfCar = [[dic objectForKey:@"data"] objectForKey:@"list"];
    //NSLog(@"%@",[[dic objectForKey:@"data"] objectForKey:@"cycleImage"]);
    self.carTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_carTableView];
    _carTableView.delegate = self;
    _carTableView.dataSource = self;
    [_carTableView registerClass:[FirstTableViewCell class] forCellReuseIdentifier:@"pool"];
    self.viewOfHeader = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 310)];
    self.carTableView.tableHeaderView = self.viewOfHeader;
    //self.carTableView.tableHeaderView.backgroundColor = [UIColor redColor];
    self.imageArr = [[dic objectForKey:@"data"] objectForKey:@"cycleImage"];
    self.marr = [NSMutableArray array];
    NSString *name = [self.imageArr[2] objectForKey:@"picCover"];
    NSURL *url = [NSURL URLWithString:name];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *image = [UIImage imageWithData:data];
    [self.marr addObject:image];
    for (int i = 0; i < self.imageArr.count ; i++) {
        NSString *name = [self.imageArr[i] objectForKey:@"picCover"];
        NSURL *url = [NSURL URLWithString:name];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *image = [UIImage imageWithData:data];
        
        if (image != nil) {
            
            [self.marr addObject:image];
        }
       
    }
    NSString *name1 = [self.imageArr[0] objectForKey:@"picCover"];
    NSURL *url1 = [NSURL URLWithString:name1];
    NSData *data1 = [NSData dataWithContentsOfURL:url1];
    UIImage *image1 = [UIImage imageWithData:data1];
    [self.marr addObject:image1];
    Album *ab = [[Album alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) withImages:self.marr];
    
    [self.viewOfHeader addSubview:ab];
    [self ctrateButton];
  [_carTableView registerClass:[SecondTableViewCell class] forCellReuseIdentifier:@"pool1"];
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)ctrateButton {
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.viewOfHeader addSubview:button1];
    button1.frame = CGRectMake(10, 210, (WIDTH - 80) / 4, (WIDTH - 80) / 4);
    [button1 setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(10, (WIDTH - 80) / 4 + 210, (WIDTH - 80) / 4 , 20)];
    [self.viewOfHeader addSubview:label1];
    label1.text = @"货款买车";
    label1.textAlignment = 1;
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.viewOfHeader addSubview:button2];
    button2.frame = CGRectMake(10 + (WIDTH - 80) / 4 + 20 , 210, (WIDTH - 80) / 4, (WIDTH - 80) / 4);
    [button2 setImage:[UIImage imageNamed:@"2"] forState:UIControlStateNormal];
    
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(10 + (WIDTH - 80) / 4 + 20 , (WIDTH - 80) / 4 + 210, (WIDTH - 80) / 4, 20)];
    [self.viewOfHeader addSubview:label2];
    label2.text = @"直销车型";
    label2.textAlignment = 1;
    
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.viewOfHeader addSubview:button3];
    button3.frame = CGRectMake(10 + (WIDTH - 80) / 2 + 40, 210, (WIDTH - 80)/4, (WIDTH - 80)/4);
    [button3 setImage:[UIImage imageNamed:@"3"] forState:UIControlStateNormal];
    UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(10 + (WIDTH - 80) / 2 + 40, (WIDTH - 80) / 4 + 210, (WIDTH - 80) / 4, 20)];
    [self.viewOfHeader addSubview:label3];
    label3.text = @"低价买车";
    label3.textAlignment = 1;
    UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.viewOfHeader addSubview:button4];
    button4.frame = CGRectMake(10 + (WIDTH - 80) * 3 / 4 + 60, 210, (WIDTH - 80)/4, (WIDTH - 80)/4);
    [button4 setImage:[UIImage imageNamed:@"4"] forState:UIControlStateNormal];
    UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(10 + (WIDTH - 80) * 3/ 4 + 60, (WIDTH - 80) / 4 + 210, (WIDTH - 80) / 4,20)];
    [self.viewOfHeader addSubview:label4];
    label4.text = @"二手车";
    
    label4.textAlignment = 1;
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrOfCar.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSNumber *i = [self.arrOfCar[indexPath.row] objectForKey:@"type"];
   
    if ([i isEqualToNumber:@3]) {
        
        SecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1" forIndexPath:indexPath];
        NSString *str = [self.arrOfCar[indexPath.row] objectForKey:@"picCover"];
        NSArray *arr = [str componentsSeparatedByString:@";"];
        NSURL *url = [NSURL URLWithString:arr[0]];
        NSURL *url1 = [NSURL URLWithString:arr[1]];
        NSURL *url2 = [NSURL URLWithString:arr[2]];
        NSData *data = [NSData dataWithContentsOfURL:url];
        NSData *data1 = [NSData dataWithContentsOfURL:url1];
         NSData *data2 = [NSData dataWithContentsOfURL:url2];
//        NSLog(@"%@",data);
    cell.imageOfFirst.image = [UIImage imageWithData:data];
        cell.imageOfSecond.image = [UIImage imageWithData:data1];
        cell.imageOfThird.image = [UIImage imageWithData:data2];
        return cell;
    }
    else {
        FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool" forIndexPath:indexPath];
        NSString *str = [self.arrOfCar[indexPath.row] objectForKey:@"picCover"];
        NSURL *url = [NSURL URLWithString:str];
        NSData *data = [NSData dataWithContentsOfURL:url];
        cell.image.image = [UIImage imageWithData:data];
        cell.labelOfTitle.text = [self.arrOfCar[indexPath.row] objectForKey:@"title"];
        return cell;
    }
   
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSNumber *i = [self.arrOfCar[indexPath.row] objectForKey:@"type"];
    if ([i isEqualToNumber:@3]){
        return  100;
    }
    else {
    return 150;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
